﻿/*
 * Copyright (c) 2009, Thiago M. Sayao <thiago.sayao@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the author nor the names of its contributors may be 
 *    used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.IO;
using Resources;
using System.Web;
using System.Configuration;
using System.Globalization;
using DotNetBurner.Core;
using DotNetBurner.Web.Settings;

namespace DotNetBurner
{
    public static class SwissKnife
    {
        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["DotNetBurnerConnectionString"].ConnectionString;
            }
        }

        public static string GetMailTemplateForLang(string template)
        {
            string lang = SiteSettings.Settings.Language;

            string path = String.Format("~/MailTemplates/{0}-{1}.txt", template, lang);
            string file = HttpContext.Current.Server.MapPath(path);

            //get the default template
            if (!File.Exists(file))
            {
                path = String.Format("~/MailTemplates/{0}.txt", template);
                file = HttpContext.Current.Server.MapPath(path);
            }

            return File.ReadAllText(file);
        }

        public static string GetSortString(int days)
        {
            switch (days)
            {
                case 0:
                    return "latest";
                case 1:
                    return "24hours";
                case 7:
                    return "7days";
                case 30:
                    return "1month";
                case 365:
                    return "1year";
            }

            return "latest";
        }

        public static string GetVoteButtonFor(string url)
        {
            string encodedUrl = url.UrlEncode();

            string voteImg = "{0}/image.axd?url={1}".FormatWith(SiteSettings.Settings.Address, encodedUrl);
            string voteUrl = "{0}/vote?url={1}".FormatWith(SiteSettings.Settings.Address, encodedUrl);

            const string format = "<a href=\"{0}\" rev=\"vote-for\" title=\"{1}\"><img src=\"{2}\" alt=\"{3}\" border=\"0\" /></a>";
            
            return format.FormatWith (voteUrl, Strings.Vote, voteImg, Strings.Vote);
        }

        public static string GetFileLastModifiedDate(string file)
        {
            CultureInfo culture = new CultureInfo(SiteSettings.Settings.Language);

            return new FileInfo(file).LastWriteTimeUtc.ToString("g", culture);
        }
    }
}
